import http from '@/api';
import { ADMIN_MODULE } from '@/api/helper/prefix';
import type { DataStatisticsResult, DriverInfo } from '@/api/types/datainfo/dataStatistics';
import type { IPage, IResultData } from '@/api/types';

// Define types for driver work details based on the actual API response
interface WorkRecord {
  id: number;
  monitorId: number;
  monitorDescription: string;
  originalPath: string;
  originalFilename: string;
  newFilename: string;
  storagePath: string;
  fileSize: number;
  fileType: string;
  fileUrl: string;
  processStatus: string;
  processStatusDisplay: string;
  errorMessage: string;
  processTime: string;
  fileMd5: string;
  fileTag: string;
  carNumber: string;
  carNumberRes: string;
  createTime: string;
  createDate: string;
  deptName: string;
  driverId: number;
  driverName: string;
}

interface GroupedWorkData {
  date: string;
  records: WorkRecord[];
}

interface DriverWorkDetailsResponse {
  current: number;
  limit: number;
  totalPage: number;
  total: number;
  rows: GroupedWorkData[];
}

// Define type for vehicle work details response
interface VehicleWorkDetailsResponse {
  current: number;
  limit: number;
  totalPage: number;
  total: number;
  rows: GroupedWorkData[];
}

/**
 * 获取数据统计信息
 * @param params 查询参数，可包含 startTime 和 endTime
 * @returns 数据统计结果
 */
export const getDataStatistics = (params?: { 
  startTime?: string; 
  endTime?: string;
  month?: string;
}) => {
  return http.get<DataStatisticsResult>(ADMIN_MODULE + `/data-statistics/statistics-count`, params);
};

/**
 * 获取司机数据列表
 * @param params 分页参数
 * @returns 司机数据列表
 */
export const getDriversList = (params: { 
  page?: number; 
  limit?: number;
  startTime?: string; 
  endTime?: string;
  month?: string;
}) => {
  return http.get<IPage<DriverInfo>>(ADMIN_MODULE + `/data-statistics/drivers`, params);
};

/**
 * 获取司机工作详情
 * @param driverId 司机ID
 * @param params 分页参数
 * @returns 司机工作详情
 */
export const getDriverWorkDetails = (driverId: number, params: { 
  page?: number; 
  limit?: number;
}) => {
  return http.get<DriverWorkDetailsResponse>(ADMIN_MODULE + `/data-statistics/work-data/driver/${driverId}/grouped-by-date`, params);
};

/**
 * 获取运输车工作详情
 * @param vehicleId 车辆ID
 * @param params 分页参数
 * @returns 运输车工作详情
 */
export const getVehicleWorkDetails = (vehicleId: number, params: { 
  page?: number; 
  limit?: number;
}) => {
  return http.get<VehicleWorkDetailsResponse>(ADMIN_MODULE + `/data-statistics/work-data/vehicle/${vehicleId}/grouped-by-date`, params);
};

/**
 * 获取钩机数据列表
 * @param params 分页参数
 * @returns 钩机数据列表
 */
export const getExcavatorsList = (params: { 
  page?: number; 
  limit?: number;
  startTime?: string; 
  endTime?: string;
  month?: string;
}) => {
  return http.get<IPage<DriverInfo>>(ADMIN_MODULE + `/data-statistics/excavators`, params);
};

/**
 * 获取运输车数据列表
 * @param params 分页参数
 * @returns 运输车数据列表
 */
export const getTransportVehiclesList = (params: { 
  page?: number; 
  limit?: number;
  startTime?: string; 
  endTime?: string;
  month?: string;
}) => {
  return http.get<IPage<DriverInfo>>(ADMIN_MODULE + `/data-statistics/transport-vehicles`, params);
};

/**
 * 获取作业数据列表
 * @param params 分页参数
 * @returns 作业数据列表
 */
export const getWorkDataList = (params: { 
  page?: number; 
  limit?: number;
  startTime?: string; 
  endTime?: string;
  month?: string;
}) => {
  return http.get<IPage<DriverInfo>>(ADMIN_MODULE + `/data-statistics/work-data`, params);
};